package com.lwj.mallchat.common.user.service.adapter;

import cn.hutool.core.bean.BeanUtil;
import com.lwj.mallchat.common.common.domain.enums.YesOrNoEnum;
import com.lwj.mallchat.common.user.domain.entity.ItemConfig;
import com.lwj.mallchat.common.user.domain.entity.User;
import com.lwj.mallchat.common.user.domain.entity.UserBackpack;
import com.lwj.mallchat.common.user.domain.vo.resp.user.BadgeResp;
import com.lwj.mallchat.common.user.domain.vo.resp.user.UserInfoResp;
import org.springframework.beans.BeanUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lwj
 * @ClassName UserAdapter
 * @date 2024/10/17 16:29
 */
public class UserAdapter {

    public static User buildUserSave(String open_id) {
        return User.builder().openId(open_id).build();
    }

    public static UserInfoResp buildUserInfo(User user, Integer modifyNameCount) {
        UserInfoResp userInfoResp = new UserInfoResp();
        BeanUtils.copyProperties(user, userInfoResp);
        userInfoResp.setModifyNameChance(modifyNameCount);
        return userInfoResp;
    }

    public static List<BadgeResp> buildBadgesResp(List<ItemConfig> itemConfigs, List<UserBackpack> userBackpacks, User user) {

        Set<Long> obtainItemSet = userBackpacks.stream().map(UserBackpack::getItemId).collect(Collectors.toSet());
        return itemConfigs.stream().map(a -> {
            BadgeResp resp = new BadgeResp();
            BeanUtil.copyProperties(a, resp);
            resp.setObtain(obtainItemSet.contains(a.getId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            resp.setWearing(Objects.equals(a.getId(), user.getId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            return resp;
        }).sorted(Comparator.comparing(BadgeResp::getWearing, Comparator.reverseOrder())
                .thenComparing(BadgeResp::getObtain, Comparator.reverseOrder()))
                .collect(Collectors.toList());


    }
}
